//
//  ViewController.m
//  Choice
//
//  Created by Mac on 2020/6/15.
//  Copyright © 2020 Mac. All rights reserved.
//

#import "ViewController.h"
#import "HomeItemView.h"
#import "AboutUsViewController.h"
#import "ChoiceViewController.h"

@interface ViewController ()

@property (nonatomic, weak)UIScrollView * scrollView;
@property (nonatomic, weak)UIView * container;
@property (nonatomic, strong)HomeItemView * eatView;
@property (nonatomic, strong)HomeItemView * drinkView;
@property (nonatomic, strong)HomeItemView * shopView;
@property (nonatomic, strong)HomeItemView * playView;
@property (nonatomic, strong)HomeItemView * smokeView;
@property (nonatomic, strong)HomeItemView * checkView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"Choice";
    self.view.backgroundColor = ZRWhiteColor;
    [self setupUI];
}


- (void)setupUI{
    
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"about"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBtnClick)];
    self.navigationItem.leftBarButtonItem = rightBtn;
    
    UIScrollView * scrollView = [[UIScrollView alloc] init];
    
    scrollView.showsVerticalScrollIndicator = NO;

    self.scrollView = scrollView;
    
    [self.view addSubview:self.scrollView];
    
    UIView * container = [UIView new];
    
    self.container = container;
    
    [self.scrollView addSubview:self.container];
    
    self.eatView = [self setupHomeItemViewWithColorstr:0xF76B84 withTitle:@"Eat" WithImage:@"eat" Withindex:1001];
    [self.scrollView addSubview:self.eatView];
    self.drinkView = [self setupHomeItemViewWithColorstr:0xFAC900 withTitle:@"Drinks" WithImage:@"drinks" Withindex:1002];
    [self.scrollView addSubview:self.drinkView];
    self.shopView = [self setupHomeItemViewWithColorstr:0x73C5E6 withTitle:@"Shopping" WithImage:@"shopping" Withindex:1003];
    [self.scrollView addSubview:self.shopView];
    self.playView = [self setupHomeItemViewWithColorstr:0x6E78F1 withTitle:@"Play" WithImage:@"play" Withindex:1004];
    [self.scrollView addSubview:self.playView];
    self.smokeView = [self setupHomeItemViewWithColorstr:0x9ED628 withTitle:@"Smoking" WithImage:@"smoking" Withindex:1005];
    [self.scrollView addSubview:self.smokeView];
    self.checkView = [self setupHomeItemViewWithColorstr:0xFF8100 withTitle:@"Check" WithImage:@"check" Withindex:1006];
    [self.scrollView addSubview:self.checkView];
}


//轻拍手势的回调方法
- (void)tapAction:(UITapGestureRecognizer*)sender{
    
    //可以根据手势得到它当前所作用的视图
    HomeItemView * itemView = (HomeItemView*)sender.view;

    ChoiceViewController * choiceVC = [ChoiceViewController new];
    
    switch (itemView.tag) {
        case 1001:

            choiceVC.type = @"eat";
            break;
        case 1002:

            choiceVC.type = @"drink";
            break;
        case 1003:

            choiceVC.type = @"shopping";
            break;
        case 1004:

            choiceVC.type = @"play";
            break;
        case 1005:

            choiceVC.type = @"smoking";
            break;
        case 1006:

            choiceVC.type = @"check";
            break;
        default:
            break;
    }
    
    [self.navigationController pushViewController:choiceVC animated:YES];
}


- (void)leftBtnClick{
    
    AboutUsViewController * aboutVC = [AboutUsViewController new];
    
    [self.navigationController pushViewController:aboutVC animated:YES];
}


-(void)viewDidLayoutSubviews{
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];
    
    [self.eatView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.scrollView).offset(LayoutW(24));
        make.top.mas_equalTo(self.scrollView).offset(30);
        make.width.mas_equalTo(LayoutW(154));
        make.height.mas_equalTo(208);
    }];
    
    [self.drinkView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.scrollView).offset(LayoutW(-24));
        make.top.mas_equalTo(self.eatView);
        make.width.mas_equalTo(LayoutW(154));
        make.height.mas_equalTo(154);
    }];
    
    [self.shopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.eatView);
        make.top.mas_equalTo(self.eatView.mas_bottom).offset(24);
        make.width.height.mas_equalTo(LayoutW(154));
    }];
    
    [self.playView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.leading.mas_equalTo(self.drinkView);
        make.top.mas_equalTo(self.drinkView.mas_bottom).offset(24);
        make.height.mas_equalTo(208);
    }];
    
    [self.smokeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.shopView);
        make.top.mas_equalTo(self.shopView.mas_bottom).offset(24);
        make.width.height.mas_equalTo(LayoutW(154));
    }];
    
    [self.checkView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.leading.mas_equalTo(self.playView);
        make.top.mas_equalTo(self.playView.mas_bottom).offset(24);
        make.height.mas_equalTo(LayoutW(154));
        make.bottom.mas_equalTo(self.container).offset(-20);
    }];
}

- (HomeItemView *)setupHomeItemViewWithColorstr:(int )color withTitle:(NSString *)title WithImage:(NSString *)img Withindex:(NSInteger)tag{
    
    HomeItemView * itemView = [HomeItemView new];
    
    itemView.backgroundColor = [UIColor hexColor:color alpha:0.3];
    itemView.titleLabel.text = title;
    itemView.iconImageView.image = [UIImage imageNamed:img];
    itemView.tag = tag;
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [itemView addGestureRecognizer:tapGR];
    
    return itemView;
}



@end
